
using namespace std;

void readFromFile(string fileName, vector<string>& lines)
{
	ifstream infile(fileName.c_str()); // open file
	string in;
	string token;

	stringstream iss;

	while (infile){

		if (infile.eof()){
                    infile.close();
        }
		else 
		{
			/************************************************
			 input from file is being put into a stringstream
			*************************************************/
			getline(infile, in);
			iss << in;


			bool eol = false;
			bool cparen = false;
			bool ccparen = false;
			bool quote = false;

			/************************************************
			 stringstream is tokenized into the token string
			 and checked for various conditions to allow
			 input to be put into vector "lines"
			 ***********************************************/
			while (getline(iss, token, ' '))
			{
				if(token[token.size()-1] == ';')	// ; signifies end of file
				{
					token.erase(token.size()-1, 1);
					eol = true;
				}

				if(token[0]=='(')
				{
					lines.push_back("(");
					token.erase(0,1);
				}

				if(token[token.size()-1]==')')
				{
					if(token[token.size()-2]==')')
					{
						if(token[0] != 'V')	// specifically for handing VARCHARs, future rewrite possible
						{
							ccparen = true;
							token.erase(token.size()-1,1);
						}
					}

					token.erase(token.size()-1,1);
					cparen = true;
				}

				if(token[token.size()-1] == ',')	//commas separate values, not needed in vector, discarded
				{
					token.erase(token.size()-1, 1);
				}

				if(token[0] == '\"' && token[token.size()-1] != '\"')	//keeps values between quotes together in vector slot
				{
					string temp = token;
					getline(iss, token, ' ');
					temp.append(" " + token);
					token = temp;
				}

				if(token[token.size()-1] == ',')	//must be called again
				{
					token.erase(token.size()-1, 1);
				}

				lines.push_back(token);	//after token passed through all above filters, finally pushed into vector

				if(cparen)
				{
					lines.push_back(")");
					cparen = false;
				}

				if(ccparen)
				{
					lines.push_back(")");
					ccparen = false;
				}
				
				if(eol)	//end of file will clear the vector to be reused
				{
					program(lines);	//send line from file in a vector to be parsed
					lines.clear();
				}
			}
			iss.clear();
		}
	}
}
